//
//  SchoolScheme.swift
//  eemall
//
//  Created by zhuxietong on 2019/5/9.
//  Copyright © 2019 zhuxietong. All rights reserved.
//

import Foundation
import soapp
import Eelay

open class SchoolBookOrderScheme:OrderSchemeSupport {
    public init() {}
    
    open var orderListfilters: [Goods.OrderListFilter]{
        get{
            return [.all,.status(.待支付, name: "待支付".loc),.status(.待发货, name: "待处理".loc),.status(.已发货, name: "预约成功".loc),.status(.已收货, name: "待评价".loc)];
        }
        //订单列表筛选配置
    }
    
    open var goodsTag: String{
        get{
            return "课程".loc
        }
    }
    
    open var sellerTag: String{
        get{
            return "学校".loc
        }
    }
    
    open var name: String{
        get{
            return "预约".loc
        }
    }
    
    open var supportSaleAfter: Bool{
        get{
            return false
        }
    }
    
    open func actionButtons(status: GoodsOrderStatus)->(ASButtonStyle,ASButtonStyle) {
        let b1 =  ASButtonStyle().hidden(hidden: true).font(size: 13).padding(insets: [5,12]).ncolor(hex: "#fff")
        let b2 =  ASButtonStyle().hidden(hidden: true).font(size: 13).padding(insets: [5,12]).ncolor(hex: "#fff")
        switch status {
        case .待发货:
            break
        case .待支付:
            _ = b1.buttonColor(hex: "#fff").hidden(hidden: false).ncolor(hex: "#999").ntitle(title: "\("取消".loc)\(OrderScheme.default.name)").set(radius: 3, width: 1, color:
                UIColor(shex:"#ccc"))
            _ = b2.buttonColor(hex: "#fff").hidden(hidden: false).ncolor(color: ___shop.tint).ntitle(title: "付款".loc).set(radius: 3, width: 1, color: ___shop.tint)
            
        case .已收货:
            _ = b1.buttonColor(hex: "#fff").hidden(hidden: false).ncolor(hex: "#999").ntitle(title: "去评价".loc).set(radius: 3, width: 1, color: UIColor(shex:"#ccc"))
        //                _ = b2.buttonColor(hex: "#fff").hidden(hidden: false).ncolor(hex: "#999").ntitle(title: "申请售后").set(radius: 3, width: 1, color: UIColor(shex:"#ccc"))
        case .评价已关闭:
            _ = b1.buttonColor(hex: "#fff").hidden(hidden: false).ncolor(hex: "#999").ntitle(title: "评价".loc).set(radius: 3, width: 1, color: UIColor(shex:"#ccc"))
            _ = b2.buttonColor(hex: "#fff").hidden(hidden: false).ncolor(hex: "#999").ntitle(title: "去评价".loc).set(radius: 3, width: 1, color: UIColor(shex:"#ccc"))
        case .已评价:
            _ = b1.buttonColor(hex: "#fff").hidden(hidden: false).ncolor(hex: "#999").ntitle(title: "评价".loc).set(radius: 3, width: 1, color: UIColor(shex:"#ccc"))
        case .已发货:
            _ = b1.buttonColor(hex: "#fff").hidden(hidden: false).ncolor(hex: "#666").ntitle(title: "确认收货".loc).set(radius: 3, width: 1, color: UIColor(shex:"#888"))
            _ = b2.buttonColor(hex: "#fff").hidden(hidden: false).ncolor(hex: "#666").ntitle(title: "查看物流".loc).set(radius: 3, width: 1, color: UIColor(shex:"#888"))
        case .已取消:
            _ = b2.buttonColor(hex: "#fff").hidden(hidden: false).ncolor(hex: "#999").ntitle(title: "删除".loc).set(radius: 3, width: 1, color: UIColor(shex:"#ccc"))
        default:
            _ = b1.buttonColor(hex: UIColor.main_hex).corner1.hidden
            _ = b2.buttonColor(hex: "#fff").hidden.set(radius: 1, width: 1, color: UIColor(shex:"#ccc"))
        }
        return (b1,b2)
        
    }
    
    open func barInfo(status: GoodsOrderStatus, obj: NSMutableDictionary) -> (name: String, color: UIColor, icon: String, info: String, bt: String) {
        let default_icon = "wddd_pingjia"
        let able_hex:UIColor = ___shop.tint
        let disable_hex:UIColor = UIColor(shex:  "#aaa")
        let verification = GoodsVerification(detail: obj)
        if verification.showVerification{
            switch verification.status{
            case .unget:
                return ("等待使用".loc,able_hex,default_icon,"","")
            case .unuse:
                return ("等待使用".loc,able_hex,default_icon,"","查看消费码".loc)
            case .timeout:
                return ("已过期".loc,disable_hex,default_icon,"","")
            case .used:
                return ("已使用".loc,able_hex,default_icon,"","查看消费码".loc)
            case .unkown:
                break
            }
        }
        
        switch status {
        case .已取消:
            return ("[XX]已取消".replaceVars(values: OrderScheme.default.name),disable_hex,default_icon,"","")
        case .待支付:
            return ("待支付".loc,able_hex,default_icon,"","立即付款".loc)
        case .支付超时:
            return ("支付超时".loc,disable_hex,default_icon,"","")
        case .待发货:
            return ("等待处理".loc,able_hex,default_icon,"","")
        case .已发货:
            return ("待确认".loc,able_hex,default_icon,"","确认".loc)
        case .已收货:
            return ("交易成功".loc,able_hex,default_icon,"交易完成，你可以去评价此次交易".loc,"去评价".loc)
        case .评价已关闭:
            return ("\(OrderScheme.default.name)\("已完成".loc)",able_hex,default_icon,"","")
        case .拼团中:
            return ("已支付，正在组团中".loc,able_hex,default_icon,"","")
        case .拼团失败:
            return ("拼团失败".loc,disable_hex,default_icon,"","")
        default:
            return ("订单已失效".loc,able_hex,default_icon,"","")
        }
    }
    
    open func doAction(with btName: String, cell: JoCellNode?, at controller: UIViewController?, order: NSMutableDictionary) {
        let orderID = order["order.id",""]
        let scheme = SaleScheme(rawValue: order["__scheme",""]) ?? .normal
        
        switch btName {
        case "确认收货".loc:
            
            JoAlertView(info: "提示".loc,"已经收到购买的相关商品？".loc,"取消".loc).append(title: "确定".loc, action: {
                HUD.share.show().mode(.indeterminate)

                JoTask().post.log.log_response().params(["orderid":orderID])._surl(scheme.confirmReceiveURL).json_handle { (status, msg, obj, resp) in
                    HUD.share.hide(animated: true)

                    if status.isSuccess{
                        __post(notify: Goods.Notice.goodsOrderChange)
                        __post(notify: Goods.Notice.groupOrderChange)
                        
                        "操作成功".loc.alert()
                        return
                    }
                    msg.alert()
                    }.run()
            }).show(at: controller)
            
        case "申请退款".loc:
            let ctr = AfterApplyStep2()
            ctr.mode = .order(order: order)
            ctr.serverType = .returnMoney
            ctr.isCanSelectGoods = false
            controller?.navigationController?.pushViewController(ctr, animated: true)
        case "申请售后".loc:
            SaleAfter.applySaleAfter(order: order, at: controller)
        case "去评价".loc:
            let ctr = GoodsCommentCtr()
            ctr.scheme = scheme
            ctr.mo.model = order
            controller?.navigationController?.pushViewController(ctr, animated: true)
        case "查看评价".loc:
            let ctr = GoodsCommentCtr()
            ctr.scheme = scheme
            ctr.mo.model = order
            controller?.navigationController?.pushViewController(ctr, animated: true)
        case "评价".loc:
            let ctr = GoodsCommentCtr()
            ctr.scheme = scheme
            ctr.mo.model = order
            controller?.navigationController?.pushViewController(ctr, animated: true)
            
        case "付款".loc,"立即付款".loc:
            let ctr = PayController()
            ctr.mo.model = order
            controller?.navigationController?.pushViewController(ctr, animated: true)
        case "删除".loc:
            JoAlertView(info: "提示".loc,"是否确定删除[XX]".replaceVars(values: OrderScheme.default.name),"取消".loc).append(title: "确定".loc, action: {
                HUD.share.show().mode(.indeterminate)

                JoTask().post.log.log_response().params(["orderid":orderID,"userdeleted":"1"])._surl(.删除订单).json_handle { (status, msg, obj, resp) in
                    HUD.share.hide(animated: true)
                    if status.isSuccess{
                        cell?.active(ID: "deleteItem")
                        return
                    }
                    msg.alert()
                    }.run()
            }).show(at: controller)
        case "取消订单".loc,"取消预约".loc:
            let ctr = OrderCancleCtr()
            ctr.mo.model = order
            ctr.canleSuccess = {
                cell?.mo.model["status"] = GoodsOrderStatus.已取消.rawValue
                cell?.mo_model_reload()
                cell?.active(ID: "canleItem")
                __post(notify: Goods.Notice.goodsOrderChange)
            }
            ctr.show()
        case "查看物流".loc:
            let ctr = LogisticsCtr()
            ctr.mo.model = order
            controller?.navigationController?.pushViewController(ctr, animated: true)
        case "查看退货".loc:
            let ctr = GoodsRefundCtr()
            ctr.mo.model = order
            controller?.navigationController?.pushViewController(ctr, animated: true)
        case "查看退款".loc:
            let ctr = MoneyRefundCtr()
            ctr.mo.model = order
            controller?.navigationController?.pushViewController(ctr, animated: true)
        case "查看消费码".loc:
            let ctr = OrderPaymentCtr()
            ctr.scheme = scheme
            ctr.mo.model = order
            controller?.navigationController?.pushViewController(ctr, animated: true)
        default:
            break
        }
    }
    
    
    
}
